package com.service.serviceImpl;

import com.mapper.OrderMapper;
import com.service.IOrderService;
import com.vo.orderVo.OrderListMapperResVo;
import com.vo.orderVo.OrderListRequestVo;
import com.vo.orderVo.OrderListResponseVo;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author hl
 * @Date
 * @Describe 订单相关功能实现
 */
@Service
public class OrderServiceImpl implements IOrderService {
    Logger logger = LoggerFactory.getLogger(IOrderService.class);

    @Resource
    OrderMapper orderMapper;

    /**
     * 获取订单列表
     * @param vo
     * @return
     */
    @Override
    public List<OrderListResponseVo> getOrderList(OrderListRequestVo vo) {

        List<OrderListMapperResVo> orderMapperList = orderMapper.getOrderList(vo);
        List<OrderListResponseVo> orderList = new ArrayList<>();

        if(orderMapperList != null){
            for(int i = 0; i <orderMapperList.size(); i++){
                OrderListResponseVo orderVo = new OrderListResponseVo();

                orderVo.setId(orderMapperList.get(i).getId());
                orderVo.setUserInfo(orderMapperList.get(i).getUserName()+orderMapperList.get(i).getUserSex()+"("+orderMapperList.get(i).getUserPhone()+")");
                orderVo.setEmployeeName(orderMapperList.get(i).getEmployeeName());
                orderVo.setPlaceName(orderMapperList.get(i).getPlaceName()+"("+orderMapperList.get(i).getBuildingId()+"号楼)");
                orderVo.setRemark(orderMapperList.get(i).getRemark());
                orderVo.setReview(orderMapperList.get(i).getReview());
                orderVo.setReviewContent(orderMapperList.get(i).getReviewContent());
                orderVo.setCreateTime(orderMapperList.get(i).getCreateTime());
                orderVo.setUpdateTime(orderMapperList.get(i).getUpdateTime());
                orderVo.setFinishTime(orderMapperList.get(i).getFinishTime());
                orderVo.setStatus(orderMapperList.get(i).getStatus());

                //遍历List<ProductList>中的商品集合，进行字符串拼接,并封装为一个List<String>
                //计算金额使用BigDecimal类型创建对象进行计算，不能使用float或者double类型进行计算，浮点型数据进行计算会造成误差。
                if(orderMapperList.get(i).getProductInfo() != null) {
                    float totalMoney = 0;
                    List<String> productInfo = new ArrayList<>();
                    for (int j = 0; j < orderMapperList.get(i).getProductInfo().size(); j++) {
                        productInfo.add(orderMapperList.get(i).getProductInfo().get(j).getProductName()+"("+orderMapperList.get(i).getProductInfo().get(j).getProductDescribe()+")"+"*"+orderMapperList.get(i).getProductInfo().get(j).getProductNum()+","+orderMapperList.get(i).getProductInfo().get(j).getProductSumPrice()+"￥");
                        float sumMoney = Float.parseFloat(orderMapperList.get(i).getProductInfo().get(j).getProductSumPrice());
                        totalMoney = totalMoney+sumMoney;
                    }
                    orderVo.setProductInfo(productInfo);
                    orderVo.setTotalMoney(String.valueOf(totalMoney));
                }
                orderList.add(orderVo);
            }
        }
        return orderList;
    }


}
